package com.financing.platform.web.api;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.Pager;
import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.dto.FpFaTotalDTO;
import com.financing.platform.dto.FpFaUserDTO;
import com.financing.platform.dto.OrderDTO;
import com.financing.platform.service.IFpFaTotalService;
import com.financing.platform.service.IFpFaUserService;
import com.financing.platform.service.IOrderService;

@Controller("fpFaTotalController")
@RequestMapping("/api/fpFaTotal")
public class FpFaTotalController extends BaseAction  { 

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IFpFaTotalService fpFaTotalService;
	@Autowired
	private IOrderService orderService;
	@Autowired
	private IFpFaUserService userService;
	
	@RequestMapping("/listFpFaTotal")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, FpFaTotalDTO fpFaTotal,Pager pager){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query fpFaTotal list begin .......");
			
			fpFaTotal.setBegin(pager.getStartIndex());
			fpFaTotal.setPageSize(pager.getPageSize());
			
			int count = fpFaTotalService.queryListCount(fpFaTotal);
			
			pager.setTotalCount(count);
			
			List<FpFaTotalDTO> fpFaTotalList = null;
			
			if(count > 0){
				fpFaTotalList = fpFaTotalService.queryList(fpFaTotal);
			}else{
				fpFaTotalList = Collections.emptyList();
			}
			result.setAttribute("pager", pager);
			result.setAttribute("list", fpFaTotalList);
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query fpFaTotal list end .......");
		}catch(Exception e){
			log.error("query fpFaTotal error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/addFpFaTotal")
	@ResponseBody
	public ResultMap add(HttpServletRequest request, FpFaTotalDTO fpFaTotal){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("add fpFaTotal  begin .......");
			
			
			int id = fpFaTotalService.insert(fpFaTotal);
			result.setAttribute("id", id);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("add fpFaTotal list end .......");
		}catch(Exception e){
			log.error("add fpFaTotal error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	
	@RequestMapping("/detailFpFaTotal.shtml")
	@ResponseBody
	public ResultMap detail(HttpServletRequest request, FpFaTotalDTO fpFaTotal){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query detail fpFaTotal  begin .......");
			
			Integer id = 1;
			BigDecimal allAmount = orderService.getAllAmount(new OrderDTO());
			FpFaTotalDTO dto = fpFaTotalService.loadById(id);
			int allUser= userService.queryUserCount(new FpFaUserDTO());
			
			result.setAttribute("dto",dto);
			result.setAttribute("allAmount",allAmount);
			result.setAttribute("allUser",allUser);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query detail fpFaTotal  end .......");
		}catch(Exception e){
			log.error("query detail fpFaTotal error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/deleteFpFaTotal")
	@ResponseBody
	public ResultMap delete(HttpServletRequest request, FpFaTotalDTO fpFaTotal){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("delete fpFaTotal  begin .......");
			
			Integer id = fpFaTotal.getId();
			
			if(id==null){
				result.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			fpFaTotalService.delete(id);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("delete fpFaTotal  end .......");
		}catch(Exception e){
			log.error("delete fpFaTotal error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/updateFpFaTotal.shtml")
	@ResponseBody
	public ResultMap update(HttpServletRequest request, FpFaTotalDTO fpFaTotal){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("update fpFaTotal  begin .......");
			
			
			fpFaTotal.setId(1);
			fpFaTotalService.update(fpFaTotal);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("update fpFaTotal list end .......");
		}catch(Exception e){
			log.error("update fpFaTotal error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/updateForceFpFaTotal")
	@ResponseBody
	public ResultMap updateForce(HttpServletRequest request, FpFaTotalDTO fpFaTotal){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("updateForce fpFaTotal  begin .......");
			
			
			fpFaTotalService.updateForce(fpFaTotal);
			
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("updateForce fpFaTotal list end .......");
		}catch(Exception e){
			log.error("updateForce fpFaTotal error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
}
